<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    let test = {
        a: 'a',
        b: [1, 2],
        c: { a: 'a', b: 'b', c: [1, 2] },
        f: function (x, y) {
            return x + y;
        }
    }

    // 对象数组原始类型深拷贝
    function deepCopy(newObj, oldObj) {
        newObj = newObj || {};
        for (let k in oldObj) {
            let item = oldObj[k];
            if (item instanceof Array) {
                newObj[k] = [];
                deepCopy(newObj[k], item);
            } else if (item instanceof Object) {
                newObj[k] = {};
                deepCopy(newObj[k], item);
            } else {
                newObj[k] = item;
            }
        }
    }

    deepCopy(obj = {}, test);
    obj.b[0] = 3;
    obj.c.c[1] = 3;
    console.log(obj, test);

    // 对象全部数据类型深拷贝
    function deepExtend(out) {
        out = out || {};
        for (var i = 1; i < arguments.length; i++) {
            var obj = arguments[i];
            if (!obj) continue;
            for (var key in obj) {
                if (obj.hasOwnProperty(key)) {
                    if (typeof obj[key] === "object" && obj[key] !== null) {
                        if (obj[key] instanceof Array) out[key] = obj[key].slice(0);
                        else out[key] = deepExtend(out[key], obj[key]);
                    } else out[key] = obj[key];
                }
            }
        }
        return out;
    }

    deepExtend(obj = {}, test);
    obj.b[0] = 3;
    obj.c.c[1] = 3;
    console.log(obj, test);
</script>

</html>